package com.wo.cloud.core.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.wo.cloud.core.constant.WocloudConstant;
import com.wo.cloud.core.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;


/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/3/25 18:10
 * Description: 定义响应结构
 */


@Data
@Getter
@NoArgsConstructor
@ApiModel(value = "统一响应消息报文")
@Accessors(chain = true)
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "状态码", required = true)
    private int code;

    @ApiModelProperty(value = "消息内容", required = true)
    private String message;

    @ApiModelProperty(value = "业务数据")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    private Response(T data) {
        this(ResultCode.SUCCESS, data, ResultCode.SUCCESS.msg);
    }


    private Response(IResultCode resultCode, String msg) {
        this(resultCode, null, msg);
    }

    private Response(IResultCode resultCode, T data, String msg) {
        this(resultCode.getCode(), data, msg);
    }

    private Response(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.message = msg;
    }

    /**
     * 返回状态码
     *
     * @return Response
     */
    public static <T> Response<T> success() {
        return new Response<T>(ResultCode.SUCCESS, "操作成功");
    }

    public static <T> Response<T> success(String msg) {
        return new Response<T>(ResultCode.SUCCESS, msg);
    }

    public static <T> Response<T> success(IResultCode resultCode, String msg) {
        return new Response<T>(resultCode, msg);
    }

    public static <T> Response<T> data(T data) {
        return new Response<T>(ResultCode.SUCCESS, data, ResultCode.SUCCESS.msg);
    }

//    public static Response data(int code, Object data, String msg) {
//        return new Response(code, data, data == null ? WocloudConstant.DEFAULT_NULL_MESSAGE : msg);
//    }

    public static <T> Response<T> fail(int code) {
        return fail(code, "操作失败");
    }

    public static <T> Response<T> fail() {
        return fail(ResultCode.FAILURE.code, "操作失败");
    }

    public static <T> Response<T> fail(String msg) {
        return fail(ResultCode.FAILURE.code, msg);
    }

    public static <T> Response<T> fail(int code, String msg) {
        return new Response<T>(code, null, msg);
    }

//    public static Response fail(IResultCode resultCode) {
//        return new Response(resultCode);
//    }

//    public static Response fail(IResultCode resultCode, String msg) {
//        return new Response(resultCode, msg);
//    }

    public static Response condition(boolean flag) {
        return flag ? success(WocloudConstant.DEFAULT_SUCCESS_MESSAGE) : fail(WocloudConstant.DEFAULT_FAIL_MESSAGE);
    }
}
